library hc_dialog_util;

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_dialog_theme.dart';
import 'package:hc_ui/widget/button/hc_text_button.dart';
import 'package:hc_ui/widget/button_panel/hc_dialog_button_panel.dart';
import 'package:hc_ui/widget/grid/hc_grid.dart';

part "../widget/dialog/hc_dialog.dart";

typedef DialogIndexFunction = void Function(int index);

class HcDialogUtil {
  const HcDialogUtil._();

  static void showMessageDialog({
    required BuildContext context,
    Widget? icon,
    String? titleText,
    Widget? title,
    required String? contentText,
    Widget? content,
    String? warningText,
    Widget? warning,
    int titleMaxLines = 3,
    double contentMinHeight = 0.0,
    double? contentMaxHeight,
    bool autoClose = true,
    HcButtonType buttonType = HcButtonType.elevated,
    bool showCloseIcon = true,
    Widget? closeIcon,
    HcDialogTheme? themeData,
    bool useNavigateBack = true,
  }) {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext dialogContext) => _HcDialog(
              iconWidget: icon,
              titleWidget:
                  title ?? (titleText != null ? Text(titleText) : null),
              contentWidget:
                  content ?? (contentText != null ? Text(contentText) : null),
              warningWidget:
                  warning ?? (warningText != null ? Text(warningText) : null),
              titleMaxLines: titleMaxLines,
              autoClose: autoClose,
              maxHeight: contentMaxHeight,
              minHeight: contentMinHeight,
              showCloseIcon: showCloseIcon,
              closeIcon: closeIcon,
              useNavigateBack: useNavigateBack,
            ));
  }

  ///
  /// 展示单个按钮的方法
  /// [context]上下文
  /// [sureText] 按钮的文字
  /// [sureButton] 自定义按钮
  /// [barrierDismissible] 点击蒙层可关闭
  /// [icon] 图标组件
  /// [titleText] 标题文字
  /// [title] 标题组件 优先级高于标题文字
  /// [contentText] 消息内容
  /// [content] 消息内容的组件优先级高于消息内容文字
  /// [warningText] 警告文字
  /// [warning] 警告文字的组件 优先级高于文字
  /// [titleMaxLines] 标题最大行数
  /// [contentMinHeight] 消息区域最小高度 默认为屏幕高度*0.2
  /// [contentMaxHeight] 消息区域最大高度 默认为屏幕高度*0.7
  /// [autoClose] 点击后自动关闭
  /// [onTap] 点击的回调
  /// [buttonType] 按钮的类型 可以选 文字 边框 实心
  /// [buttonBackground] 按钮的背景颜色 仅针对实心有效
  /// [buttonRadius] 按钮的圆角
  /// [themeData] 弹窗的主题设置
  static void showAlertDialog({
    required BuildContext context,
    required String sureText,
    bool barrierDismissible = false,
    Widget? icon,
    String? titleText,
    Widget? title,
    String? contentText,
    Widget? content,
    String? warningText,
    Widget? warning,
    Widget? sureButton,
    int titleMaxLines = 3,
    double? contentMinHeight,
    double? contentMaxHeight,
    bool autoClose = true,
    GestureTapCallback? onTap,
    HcButtonType buttonType = HcButtonType.elevated,
    Color? buttonBackground,
    double? buttonRadius,
    Axis buttonDecoration = Axis.horizontal,
    bool showCloseIcon = false,
    bool useNavigateBack = true,
    Widget? closeIcon,
    HcDialogTheme? themeData,
  }) {
    showDialog<void>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (BuildContext dialogContext) => _HcDialog(
              iconWidget: icon,
              titleWidget:
                  title ?? (titleText != null ? Text(titleText) : null),
              contentWidget:
                  content ?? (contentText != null ? Text(contentText) : null),
              warningWidget:
                  warning ?? (warningText != null ? Text(warningText) : null),
              themeData: themeData,
              minHeight: contentMinHeight,
              maxHeight: contentMaxHeight,
              titleMaxLines: titleMaxLines,
              buttonDecoration: buttonDecoration,
              buttonTextList: [
                sureText,
              ],
              autoClose: autoClose,
              buttonRadius: buttonRadius,
              mainButtonBackground: buttonBackground,
              indexFunction: (index) {
                if (onTap != null && index == 0) {
                  onTap();
                }
              },
              mainButtonType: buttonType,
              buttonList: sureButton != null ? [sureButton] : null,
              showCloseIcon: showCloseIcon,
              closeIcon: closeIcon,
              useNavigateBack: useNavigateBack,
            ));
  }

  /// 展示确认取消按钮弹窗的方法
  /// 如果sureBtnType和cancelBtnType都为Text则Padding为0 展示带分隔符的按钮
  /// 如果cancelBtnType为Text则按钮纵向排列,
  static void showConfirmBtnDialog(
      {required BuildContext context,
      required String sureText,
      required String cancelText,
      required GestureTapCallback? sureBtnOnTap,
      required GestureTapCallback? cancelBtnOnTap,
      bool barrierDismissible = false,
      bool buttonGroupReverse = false,
      Widget? icon,
      String? titleText,
      Widget? title,
      String? contentText,
      Widget? content,
      String? warningText,
      Widget? warning,
      double? contentMinHeight,
      double? contentMaxHeight,
      int titleMaxLines = 3,
      bool autoClose = true,
      HcDialogTheme? themeData,
      double? buttonRadius,
      Color? sureBtnColor,
      Color? cancelBtnColor,
      HcButtonType sureBtnType = HcButtonType.elevated,
      HcButtonType cancelBtnType = HcButtonType.outlined,
      Axis buttonDecoration = Axis.horizontal,
      bool showCloseIcon = false,
      Widget? closeIcon,
      Widget? sureButton,
      bool useNavigateBack = true,
      Widget? cancelButton}) {
    List<Widget> buttonList = [];
    if (sureButton != null) {
      buttonList.add(sureButton);
    }
    if (cancelButton != null) {
      buttonList.add(cancelButton);
    }
    showDialog<void>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (BuildContext dialogContext) => _HcDialog(
              iconWidget: icon,
              buttonGroupReverse: buttonGroupReverse,
              titleWidget:
                  title ?? (titleText != null ? Text(titleText) : null),
              contentWidget:
                  content ?? (contentText != null ? Text(contentText) : null),
              warningWidget:
                  warning ?? (warningText != null ? Text(warningText) : null),
              secondaryButtonBackground: cancelBtnColor,
              themeData: themeData,
              minHeight: contentMinHeight,
              maxHeight: contentMaxHeight,
              titleMaxLines: titleMaxLines,
              buttonTextList: [sureText, cancelText],
              autoClose: autoClose,
              buttonRadius: buttonRadius,
              buttonDecoration: buttonDecoration,
              mainButtonBackground: sureBtnColor,
              indexFunction: (index) {
                if (sureBtnOnTap != null && index == 0) {
                  sureBtnOnTap();
                }
                if (cancelBtnOnTap != null && index == 1) {
                  cancelBtnOnTap();
                }
              },
              mainButtonType: sureBtnType,
              secondaryButtonType: cancelBtnType,
              buttonList: buttonList.isNotEmpty ? buttonList : null,
              showCloseIcon: showCloseIcon,
              closeIcon: closeIcon,
              useNavigateBack: useNavigateBack,
            ));
  }

  static void showMultipleBtnDialog() {}

  static void showCustomDialog() {}
}
